﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Windows.Forms;
using Assist;
using DataBase;
using MySql.Data.MySqlClient;

namespace YJKDataLookAfter
{
	public class BimServerDB : IDatabase
	{
		public static BimServerDB Default
		{
			get
			{
				string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(BimServerDB.ConfigName, "Server", "value");
				string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(BimServerDB.ConfigName, "DbName", "value");
				string dataFromConfigFile3 = XmlUtils.GetDataFromConfigFile(BimServerDB.ConfigName, "Uid", "value");
				string dataFromConfigFile4 = XmlUtils.GetDataFromConfigFile(BimServerDB.ConfigName, "Pwd", "value");
				BimServerDB result;
				try
				{
					result = new MySqlDB(string.Format("Server ={0};Port={1};Charset=utf8;Database ={2}; Uid ={3}; Pwd ={4};", new object[]
					{
						dataFromConfigFile,
						"3336",
						dataFromConfigFile2,
						dataFromConfigFile3,
						dataFromConfigFile4
					}));
				}
				catch (Exception ex)
				{
					throw new Exception("服务器连接异常,请运行服务器连接配置设置服务器。\n" + ex.Message);
				}
				return result;
			}
		}

		public DbConnection Connection
		{
			get
			{
				return this.conn;
			}
			set
			{
				this.conn = value;
			}
		}

		public void Open()
		{
			try
			{
				this.conn.Open();
			}
			catch (Exception ex)
			{
				throw new Exception("连接失败，请检查数据库服务器是否正常运行。\n" + ex.Message);
			}
		}

		public DbTransaction StartTransaction()
		{
			if (this.Connection.State == ConnectionState.Closed)
			{
				this.Connection.Open();
			}
			return this.conn.BeginTransaction();
		}

		public void CommitTransaction(DbTransaction tr)
		{
			if (this.Connection.State == ConnectionState.Closed)
			{
				this.Connection.Open();
			}
			tr.Commit();
		}

		public void RollbackTransaction(DbTransaction tr)
		{
			if (this.Connection.State == ConnectionState.Closed)
			{
				this.Connection.Open();
			}
			tr.Rollback();
		}

		public bool ExecuteCommand(string sql)
		{
			bool result;
			try
			{
				DbCommand dbCommand = this.conn.CreateCommand();
				dbCommand.CommandText = sql;
				dbCommand.ExecuteNonQuery();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool ExecuteCommand(string sql, DbTransaction tr)
		{
			bool result;
			try
			{
				DbCommand dbCommand = this.conn.CreateCommand();
				dbCommand.CommandText = sql;
				dbCommand.ExecuteNonQuery();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public DataTable ExecQuery(string sqlStr)
		{
			DataTable result;
			try
			{
				new DataTable();
				using (DbCommand dbCommand = this.conn.CreateCommand())
				{
					dbCommand.CommandText = sqlStr;
					dbCommand.CommandType = CommandType.Text;
					IDataReader dataReader = dbCommand.ExecuteReader();
					DataTable dataTable = this.DataReaderToDataTable(dataReader);
					dataReader.Dispose();
					result = dataTable;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public DataTable DataReaderToDataTable(IDataReader dataReader)
		{
			DataTable dataTable = new DataTable();
			DataTable schemaTable = dataReader.GetSchemaTable();
			DataTable result;
			try
			{
				for (int i = 1; i <= schemaTable.Rows.Count; i++)
				{
					dataTable.Columns.Add(schemaTable.Rows[i - 1]["ColumnName"].ToString(), Type.GetType(schemaTable.Rows[i - 1]["DataType"].ToString()));
				}
				while (dataReader.Read())
				{
					DataRow dataRow = dataTable.NewRow();
					for (int j = 0; j < schemaTable.Rows.Count; j++)
					{
						dataRow[j] = dataReader[j];
					}
					dataTable.Rows.Add(dataRow);
				}
				result = dataTable;
			}
			catch (Exception ex)
			{
				result = dataTable;
			}
			return result;
		}

		public static bool TestConnect(string server, string dbName, string userName, string password, string port, bool isSqlServer = false)
		{
			if (isSqlServer)
			{
				SqlConnection sqlConnection = new SqlConnection();
				try
				{
					string connectionString = string.Format("Server ={0};Database ={1}; Uid ={2}; Pwd ={3};", new object[]
					{
						server,
						dbName,
						userName,
						password
					});
					sqlConnection.ConnectionString = connectionString;
					sqlConnection.Open();
					sqlConnection.Close();
					sqlConnection.Dispose();
					return true;
				}
				catch (Exception ex)
				{
					if (ex.GetType() == typeof(MySqlException))
					{
						MySqlException ex2 = ex as MySqlException;
						if (ex2.Number == 1042)
						{
							MessageBox.Show("连接失败\n服务器地址错误");
							return false;
						}
						if (ex2.Number == 1045)
						{
							MessageBox.Show("连接失败\n用户名或密码错误");
							return false;
						}
					}
					MessageBox.Show("连接失败\n" + ex.Message);
					return false;
				}
			}
			bool result;
			try
			{
				MySqlConnection mySqlConnection = new MySqlConnection();
				string connectionString2 = string.Format("Server ={0};Port={1};Charset=utf8;Database ={2}; Uid ={3}; Pwd ={4};", new object[]
				{
					server,
					port,
					dbName,
					userName,
					password
				});
				mySqlConnection.ConnectionString = connectionString2;
				mySqlConnection.Open();
				mySqlConnection.Close();
				mySqlConnection.Dispose();
				result = true;
			}
			catch (Exception ex3)
			{
				if (ex3.GetType() == typeof(MySqlException))
				{
					MySqlException ex4 = ex3 as MySqlException;
					if (ex4.Number == 1042)
					{
						MessageBox.Show("连接失败\n服务器地址错误");
						return false;
					}
					if (ex4.Number == 1045)
					{
						MessageBox.Show("连接失败\n用户名或密码错误");
						return false;
					}
				}
				MessageBox.Show("连接失败\n" + ex3.Message);
				result = false;
			}
			return result;
		}

		public void Close()
		{
			this.conn.Close();
		}

		public static string ConfigName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";

		protected DbConnection conn;
	}
}
